<!DOCTYPE html>
<html>
<!--
  Copyright 2018 The Chromium Authors. All rights reserved.
  Use of this source code is governed by a BSD-style license that can be
  found in the LICENSE file.
-->
<head>
<!-- Create a schema with a blob.  This will be downgraded to V2 by the
     invoking browsertest. -->
<title>IDB test for v2schema corruption - setup</title>
<script type="text/javascript" src="common.js"></script>
<script>

function test() {
  // TODO(cmp): Switch this and the other corruption browser test to a promise-
  //            based call flow.
  indexedDBTest(upgradeCallback, openCallback);
}

let db;

function upgradeCallback() {
  db = event.target.result;
  deleteAllObjectStores(db);
  objectStore = db.createObjectStore('storeName', { autoIncrement : true });
}

function openCallback() {
  const transaction = db.transaction('storeName', 'readwrite');
  transaction.onabort = unexpectedAbortCallback;
  transaction.onerror = unexpectedErrorCallback;
  transaction.oncomplete = getBlob;
  const objectStore = transaction.objectStore('storeName');
  const request = objectStore.put({blob: new Blob(['abc'])}, 'key-0');
  request.onerror = unexpectedErrorCallback;
}

function getBlob() {
  const transaction = db.transaction('storeName', 'readwrite');
  transaction.onabort = unexpectedAbortCallback;
  transaction.onerror = unexpectedErrorCallback;
  const objectStore = transaction.objectStore('storeName');
  const request = objectStore.get('key-0');
  request.onerror = unexpectedErrorCallback;
  request.onsuccess = () => {
    const reader = new FileReader();
    reader.addEventListener("loadend", () => {
      if (reader.result !== "abc") {
        fail(`expected blob to contain 'abc', got '${reader.result}'`);
        return;
      }
      done();
    });
    reader.readAsText(request.result.blob);
  }
}

</script>
</head>
<body onLoad="test()">
<div id="status">Starting...</div>
</body>
</html>
